<template>
  <div class="date-status">
    <el-row :gutter="15" type="flex" justify="center" style="width:100%;margin-bottom:20px;">
      <el-col :span="4">
        <div class="list">
          <p style="font-size:32px;margin:0;">{{ reserve }}</p>
          <span>可预约课次</span>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="list">
          <p style="font-size:32px;margin:0;">{{ bespeak }}</p>
          <span>已预约课次</span>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="list">
          <p style="font-size:32px;margin:0;">{{ finish }}</p>
          <span>已完成课次</span>
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="4" type="flex" justify="start" class="search">
      <el-col :span="24">
        <el-select v-model="value1" @change="handleFilter">
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select v-model="value2" @change="handleFilter">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-input v-model="keyWord" placeholder="关键词" style="width:200px;" @keyup.enter.native="handleFilter"/>
        <el-button type="primary" @click="handleFilter">搜索</el-button>
        <el-button size="medium" type="info" @click="clearParams">清空</el-button>
      </el-col>
    </el-row>
    <eltable
      :table-label="tableLabel"
      :table-data="tableData.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    >
      <el-table-column slot="isPro" label="老师名称" align="center" min-width="15">
        <template slot-scope="scope">
          <el-button type="text">{{ scope.row.teacherName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column slot="isEnd" label="学员名称" align="center" min-width="15">
        <template slot-scope="scope">
          <el-button type="text">{{ scope.row.studentName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column slot="button" label="上课时间" align="center" min-width="40">
        <template slot-scope="scope">
          <span>{{ scope.row.classTime }}</span>
        </template>
      </el-table-column>
    </eltable>
    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
  </div>
</template>

<script>
import Eltable from '@/components/Eltable'
import Pagination from '@/components/Pagination'
import { getArrayCourseCount, getBespeakCourse } from '@/api/subject/course' // 数据接口
import { getTenantId } from '@/utils/auth'
export default {
  components: {
    Eltable,
    Pagination
  },
  data() {
    return {
      total: 3,
      currentpage: 1,
      pagesize: 10,
      reserve: '',
      bespeak: '',
      finish: '',
      tableLabel: [
        {
          label: '课程名称', width: '40', prop: 'arrayCourseName'
        },
        {
          label: '状态', width: '10', prop: 'statusStr'
        },
        {
          label: '预约时间', width: '25', prop: 'createTimeStr'
        }
      ],
      tableData: [],
      listFilter: [],
      options1: [
        {
          label: '全部',
          value: ''
        },
        {
          label: '已预约',
          value: '已预约'
        },
        {
          label: '已完成',
          value: '已完成'
        },
        {
          label: '学员取消预约',
          value: '学员取消预约'
        },
        {
          label: '老师取消预约',
          value: '老师取消预约'
        }
      ],
      options2: [
        {
          label: '搜索条件',
          value: ''
        },
        {
          label: '课程名称',
          value: '课程名称'
        },
        {
          label: '教师名称',
          value: '教师名称'
        },
        {
          label: '学员名称',
          value: '学员名称'
        }
      ],
      value1: '',
      value2: '',
      keyWord: ''
    }
  },
  created() {
    this.getData()
  },
  updated() {
    this.statusfil()
  },
  methods: {
    handleSizeChange(val) {
      this.currentpage = 1
      this.pagesize = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleCurrentChange(val) {
      this.currentpage = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    statusfil() {
      this.bespeak = this.listFilter.filter((item) => { return item.status === 'bespeak' }).length
      this.finish = this.listFilter.filter((item) => { return item.status === 'finish' }).length
    },
    getData() {
      getBespeakCourse(getTenantId()).then(res => {
        res.data.items.map(item => {
          if (item.createTime) item.createTimeStr = new Date(item.createTime).toLocaleString().replace(/[\u4e00-\u9fa5]/g, '')
          if (item.status === 'bespeak') item.statusStr = '已预约'
          if (item.status === 'finish') item.statusStr = '已完成'
          if (item.status === 'stuCancle') item.statusStr = '学员取消预约'
          if (item.status === 'teaCancle') item.statusStr = '老师取消预约'
          if (item.startTime) item.startTimeStr = new Date(item.startTime).toLocaleString().replace(/[\u4e00-\u9fa5]/g, '')
          if (item.endTime) item.endTimeStr = new Date(item.endTime).toLocaleString().replace(/[\u4e00-\u9fa5]/g, '')
          if (item.startTimeStr && item.endTimeStr) item.classTime = `${item.startTimeStr} ~ ${item.endTimeStr}`
        })
        this.listFilter = res.data.items
        this.tableData = res.data.items
        this.total = this.tableData.length
      })
      getArrayCourseCount().then(res => {
        this.reserve = res.data.item
      })
    },
    handleFilter() {
      this.tableData = this.listFilter.filter(item => {
        if (this.value1 && item.statusStr !== this.value1) return false
        if (this.value2 === '课程名称' && this.keyWord && item.arrayCourseName.toLocaleLowerCase().indexOf(this.keyWord.toLocaleLowerCase()) === -1) return false
        if (this.value2 === '教师名称' && this.keyWord && item.teacherName.toLocaleLowerCase().indexOf(this.keyWord.toLocaleLowerCase()) === -1) return false
        if (this.value2 === '学员名称' && this.keyWord && item.studentName.toLocaleLowerCase().indexOf(this.keyWord.toLocaleLowerCase()) === -1) return false
        return true
      })
      this.total = this.tableData.length
    },
    clearParams() {
      this.value1 = ''
      this.value2 = ''
      this.keyWord = ''
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
.date-status {
  margin-top: 0px;
  width: 100%;
  // padding: 14px 20px 21px 20px;
  .list {
    height: 110px;
    text-align: center;
    padding-top: 26px;
    box-shadow: 0px 1px 6px rgba(0, 0, 0, 0.4);
    border-radius: 5px;
  }
  .search {
    margin-top: 5px;
    padding: 15px 20px;
    background-color: #e5e7ea;
    border-radius: 5px;
  }
}
</style>
